﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using AssetLoad;

namespace Room
{
    public class CacheFrame
    {
        public Mesh mesh;
        public Matrix4x4 leftMatrix;
        public Matrix4x4 rightMatrix;
    }
    public class RoomMeshFramePool : MonoBehaviour
    {

        public const float FRAME_DELTA = 0.1f;

        #region Instance
        private static RoomMeshFramePool instance;
        public static RoomMeshFramePool Instance
        {
            get
            {
                return instance;
            }
        }
        void Awake()
        {
            instance = this;
        }
        #endregion

        private Dictionary<string, Dictionary<string, CacheFrame[]>> cache = new Dictionary<string, Dictionary<string, CacheFrame[]>>();
        private Dictionary<string, int> modelRefrences = new Dictionary<string, int>();
#if UNITY_EDITOR
        public List<string> listSavedFrame = new List<string>();
#endif
        void Start()
        {
            Init();
        }
        public void Init()
        {
        }
        public void UnloadUnusedFrame()
        {
            foreach(var pair in modelRefrences)
            {
                if(pair.Value==0)
                {
                    cache[pair.Key] = null;
                }
            }
        }
        public void RetainModel(string modelName)
        {
            if (!cache.ContainsKey(modelName))
                cache.Add(modelName, new Dictionary<string, CacheFrame[]>());
            if (!modelRefrences.ContainsKey(modelName))
                modelRefrences.Add(modelName, 0);
            modelRefrences[modelName]= modelRefrences[modelName] + 1;
        }
        public void ReleaseModel(string modelName)
        {
            modelRefrences[modelName] = modelRefrences[modelName] - 1;
        }
        public void CreateAnimation(string modelName,string animName, int frameCount)
        {
            if(!cache[modelName].ContainsKey(animName))
                cache[modelName].Add(animName, new CacheFrame[frameCount]);
        }
        public CacheFrame GetFrame(string modelName,string animName,int frame)
        {
            return cache[modelName][animName][frame];
        }
        public void PutFrame(string modelName,string animName,int frame,CacheFrame cacheFrame)
        {
            cache[modelName][animName][frame] = cacheFrame;
#if UNITY_EDITOR
            listSavedFrame.Add(modelName + ";" + animName + ";" + frame);
#endif
        }
        //public class CacheModel
        //{
        //    //public 
        //}

//        public class CacheAnimation
//        {
//            public string modelName;
//            public string animName;
//            public float length;
//            private bool saveCompleted;
//            private List<CacheFrame> frames = new List<CacheFrame>();
//            public CacheAnimation()
//            {
//                modelName = "";
//                animName = "";
//                length = 0;

//            }
//            public bool IsSaveComplete()
//            {
//                return saveCompleted;
//            }
//            public void Save(float time, Mesh mesh)
//            {
//                frames.Add(new CacheFrame(time, mesh));
//                if (time > length)
//                    saveCompleted = true;
//#if UNITY_EDITOR
//                RoomMeshFramePool.Instance.listSavedFrame.Add(modelName + ":" + animName + ":" + time);
//#endif
//            }
//            public bool NeedSave(float time)
//            {
//                if (frames.Count > 0)
//                {
//                    return (time - frames[frames.Count - 1].time) > FRAME_DELTA;
//                }
//                return true;
//            }
//            public CacheFrame GetFrame(int i)
//            {
//                if (i >= frames.Count)
//                    return null;
//                return frames[i];
//            }
//        }

    }
}
